package org.zjh.controller.listener;


import org.zjh.constant.GameConstant;
import org.zjh.controller.GameController;
import org.zjh.model.ComputerA;
import org.zjh.model.ComputerB;
import org.zjh.model.ComputerPlayer;
import org.zjh.utils.RandomUtil;
import org.zjh.view.game.GameFrame;
import org.zjh.view.ui.GameUi;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @description: 游戏开始按钮监听
 * @author 郑佳豪
 * @date 2024/10/22 1:53
 * @version 1.0
 */
public class StartGameJbtLis extends MouseAdapter {


    @Override
    public void mouseClicked(MouseEvent e) {
        GameController gameController = GameController.getInstance();
        // 检查 GameFrame 是否已存在
        if (GameUi.gameFrame == null) {
            // 创建电脑玩家 A 和 B
            ComputerPlayer computerA = new ComputerA(GameConstant.COMPUTER_NAMES[RandomUtil.RANDOM.nextInt(GameConstant.COMPUTER_NAMES.length)], 100);
            ComputerPlayer computerB = new ComputerB(GameConstant.COMPUTER_NAMES[RandomUtil.RANDOM.nextInt(GameConstant.COMPUTER_NAMES.length)], 100);

            // 设定轮询机制
            computerA.setNextPlayer(gameController.getPlayer());
            gameController.getPlayer().setNextPlayer(computerB);
            computerB.setNextPlayer(computerA);

            // 设置电脑玩家
            gameController.setComputerA(computerA);
            gameController.setComputerB(computerB);
            gameController.setCurrentPlayer(computerA);

            // 创建游戏窗口
            GameUi.gameFrame = new GameFrame();
        }

    }
}
